<template>
  <t-filed :data="data" :withPop="1==1" minHeight="390px" :active="1==1" @onchange="onchange" @onsubmit="onsubmit" title="请选择商品类型" @oncancel="oncancel"></t-filed>
</template>
<script>
  import tool from 'src/util/tool';
  import util from '_util/index';
  import logic from '_logic';

  import tFiled from 'components/tunit/tFields'
  export default {
    data() {
      return {
        query: {},
        data: [
          [{
            label: "一级",
            show_type: "select",
            name: "level1",
            value: "",
            // element: [{label:"必填", value:1 },{label: "选填", value:0}]
            element: []
          },{
            label: "二级",
            show_type: "select",
            ishide: true,
            name: "level2",
            value: "",
            // element: [{label:"必填", value:1 },{label: "选填", value:0}]
            element: []
          },{
            label: "三级",
            show_type: "select",
            name: "level3",
            ishide: true,
            value: "",
            // element: [{label:"必填", value:1 },{label: "选填", value:0}]
            element: []
          }]
        ],
        params:{
          data:{
          },
          show:{}
        },

        tips:{
          type_id:"请选择商品类型",
          level1: "请选择一级",
          level2: "请选择二级",
          level3: "请选择三级",
        }
      }
    },
    methods:{
      /* 数据操作 */
      getData({url=[], data={}, callback=null, error=false, msg=null}){
        url = tool.getApi(this, url[0], url[1]);
        util.http.SR(this, url, data, msg, callback, error);
      },
      onchange(filed){
        let {name, value, text} = filed;
        this.params.data[name] = value;
        if(~['level1', 'level2'].indexOf(name) && value){
          this.getLevel( name.split("level")[1]*1 + 1 );
        }
        this.params.show[name] = filed;
        this.$emit("onchange", this.params);
      },
      checkData(data){
        let _this = this,
            name = "";
        data.forEach((item) => {
          if(name){return;}
          if(Array.isArray(item)){
            name = _this.checkData( item );
            return;
          }
          if(item.element.length>0){
            if(!this.params.data[ item.name ]){
              name = item.name;
            }else if( !this.params.data[ item.name ]){
              name = item.name;
            }
          }
        });
        return name;
      },
      onsubmit(form){
        let isOk = true,
            name = this.checkData(this.data);
        if(name){
          util.showMsg(this, 'error', this.tips[name]);
          return;
        };
        this.$emit("onsubmit", this.params);
      },
      oncancel(){
        this.$emit("oncancel");
      },
      setValue({name, data=[]}){
        let _data= [];
        data.forEach((item) => {
          let res = {};
          res.label = item.title;
          res.value = item.id;
          _data.push(res);
        });
        this.data.map((item) => {
          if(Array.isArray(item)){
            item.forEach((_item) => {
                if(_item.name == name){
                  _item.ishide = (_data||[]).length == 0
                  _item.element = _data;
                }
                return _item
            })
          }else if(item.name == name){
            item.element = _data
          }
          return item;
        });
        // console.log(this.data);
      },
      getType(){
        let _this = this;
        this.data.unshift({
            label: "商品类型",
            show_type: "select",
            name: "type_id",
            element: [],
        })
        this.getData({
          url:['ProductType', 'getType'],
          callback(rs){
            _this.setValue({name:"type_id", data:rs});
          }
        })
      },
      getLevel(level=1){
        let data = {level: level};
        if(level!=1){
          data.parent_id = this.params.data[ "level"+ (level - 1) ];
        }
        let _this = this;
        this.getData({
          url:['Navigation', 'getNav'],
          data: data,
          callback(rs){
            if(level == 2){
              _this.setValue({name: "level2", data:[]});
              _this.setValue({name: "level3", data:[]});
            }
            if(level == 3){
              _this.setValue({name: "level3", data:[]});
            }
            _this.setValue({name:"level"+level, data:rs});

          }
        })
      },
    },
    props:{
      isEditType:{
        type: Boolean,
        default: false
      }
    },
    components:{
      tFiled
    },
    computed: {
    },
    created() {
      this.query = this.$route.query;
      if(!this.isEditType){
        this.getType();
      }
      this.getLevel();
    }
  }
</script>
<style rel="stylesheet/less" scoped lang="less">
</style>
